package org.neusoft.controller;

import org.neusoft.dto.Article;
import org.neusoft.dto.ArticleCondition;
import org.neusoft.dto.ArticleInfo;
import org.neusoft.entity.Articles;
import org.neusoft.mapper.ArticlesMapper;
import org.neusoft.rsp.ApiResponse;
import org.neusoft.service.IArticleService;
import org.neusoft.utils.JwtService;
import org.neusoft.utils.UserUtils;
import org.neusoft.vo.ArticleInfoVo;
import org.neusoft.vo.ArticlesVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/13 16:26
 * @DESCRIBE
 */
@RestController
@RequestMapping("/api")
public class ArticleController {

    @Resource
    IArticleService iarticleService;

    @Resource
    JwtService jwtService;

    @GetMapping("/articles")
    public ApiResponse<ArticlesVo> getAllArticles(ArticleCondition articleCondition){
        if(articleCondition.getLimit() == null){
            articleCondition.setLimit(20);
        }
        if (articleCondition.getOffset() == null){
            articleCondition.setOffset(0);
        }
        ArticlesVo articles = iarticleService.getAllArticles(articleCondition);
        return ApiResponse.success(articles);
    }

    @GetMapping("/articles/{aid}")
    public ApiResponse<ArticleInfoVo> getArticleDetail(@PathVariable Long aid){
        ArticleInfoVo articleInfoVo = iarticleService.getArticleDetail(aid);
        return ApiResponse.success(articleInfoVo);
    }

    @PostMapping("/articles")
    public String createArticle(@RequestBody ArticleInfo article, HttpServletRequest httpServletRequest){
//        String token = httpServletRequest.getHeader("Authorization");
//        JwtService jwtService = new JwtService();
//        String userId = null;
//        if(jwtService.validateToken(token))
//        {
//            userId = jwtService.getSub(token);
//        }
        System.out.println(article);
        long loginUserId = UserUtils.getLoginUserId();
        iarticleService.createArticle(article,loginUserId);

//        iarticleService.foo();
        return "ok";
    }
}
